package com.os.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.os.dao.FoodDaoX;
import com.os.model.FoodX;
import com.os.util.JdbcConUtilM;


public class FoodDaoImpX implements FoodDaoX {


	@Override
	public List selectFood(int start, int pageSize) {
		// TODO Auto-generated method stub
		List<FoodX> li = new ArrayList<FoodX>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from o_food");
		sql.append(" where fhasma = 1");
		sql.append(" LIMIT ? , ? ");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement ps =null;
		 /* Date date=new Date();
		  DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  String time=format.format(date);
		  System.out.println(time);*/
		try {
			 ps = con.prepareStatement(sql.toString());
			ps.setInt(1, (start-1)*pageSize);
			ps.setInt(2, pageSize);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				FoodX food = new FoodX();
				food.setFid(rs.getInt("fid"));
				food.setFname(rs.getString("fname"));
				food.setFtype(rs.getString("ftype"));
				food.setFspecialty(rs.getInt("fspecialty"));
				food.setFurl(rs.getString("furl"));
				food.setFnew(rs.getInt("fnew"));
				food.setFprice(rs.getDouble("fprice"));
				food.setFmaterial(rs.getString("fmaterial"));
				food.setFhasma(rs.getInt("fhasma"));
				li.add(food);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcConUtilM.closePstAndCon(ps, con);
		}
		return li;
	}

	@Override
	public int getFoodCount() {
		// TODO Auto-generated method stub
		int flag=0;
		StringBuffer sql = new StringBuffer();
		sql.append("select COUNT(fid) scount from o_food");
		sql.append(" where fhasma = 1");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement ps = null;
		try {
			 ps = con.prepareStatement(sql.toString());
			ResultSet rs =  ps.executeQuery();
			if(rs.next()){
				flag = rs.getInt("scount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcConUtilM.closePstAndCon(ps, con);
		}
		return flag;
	}

	@Override
	public List selectFoodByFtype(String ftype, int start, int pageSize) {
		List<FoodX> list = new ArrayList<FoodX>();
		StringBuffer sql = new StringBuffer();
		if(ftype.equals("1")){
			sql.append("SELECT * FROM o_food");
			sql.append(" WHERE fspecialty = ? and fhasma = 1");/*and fhasma = 0");
	*/		sql.append(" LIMIT ? , ?");
			
		}else{
		sql.append("SELECT * FROM o_food");
		sql.append(" WHERE ftype = ? and fhasma = 1");/*and fhasma = 0");
*/		sql.append(" LIMIT ? , ?");}

		Connection con = JdbcConUtilM.getCon();
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement(sql.toString());
			ps.setString(1, ftype);
			ps.setInt(2, (start-1)*pageSize);
			ps.setInt(3, pageSize);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				FoodX food = new FoodX();
				food.setFid(rs.getInt("fid"));
				food.setFname(rs.getString("fname"));
				food.setFtype(rs.getString("ftype"));
				food.setFspecialty(rs.getInt("fspecialty"));
				food.setFurl(rs.getString("furl"));
				food.setFnew(rs.getInt("fnew"));
				food.setFprice(rs.getDouble("fprice"));
				food.setFmaterial(rs.getString("fmaterial"));
				food.setFhasma(rs.getInt("fhasma"));
				list.add(food);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcConUtilM.closePstAndCon(ps, con);
		}
		
		return list;
	}

	@Override
	public int getFoodCountBytype(String type) {
		int flag=0;
		StringBuffer sql = new StringBuffer();
		if(type.equals("1")){
			sql.append("select COUNT(fid) scount from o_food");
			sql.append(" WHERE fspecialty =? and fhasma = 1");
		}else{
		sql.append("select COUNT(fid) scount from o_food");
		sql.append(" WHERE ftype =? and fhasma = 1");} /*and fhasma = 0"
*/		Connection con = JdbcConUtilM.getCon();
		PreparedStatement ps = null;
		try {
			 ps = con.prepareStatement(sql.toString());
			 ps.setString(1, type);
			ResultSet rs =  ps.executeQuery();
			if(rs.next()){
				flag = rs.getInt("scount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcConUtilM.closePstAndCon(ps, con);
		}
		return flag;
	}

	@Override
	public List selectLike(String like, int start, int pageSize) {
		List<FoodX> li = new ArrayList<FoodX>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from o_food");
		sql.append(" where fname LIKE ? and fhasma = 1"); /*and fhasma = 0*/
		sql.append(" LIMIT ? , ? ");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement ps =null;
		try {
			 ps = con.prepareStatement(sql.toString());
			 ps.setString(1, "%"+like+"%");
			ps.setInt(2, (start-1)*pageSize);
			ps.setInt(3, pageSize);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				FoodX food = new FoodX();
				food.setFid(rs.getInt("fid"));
				food.setFname(rs.getString("fname"));
				food.setFtype(rs.getString("ftype"));
				food.setFspecialty(rs.getInt("fspecialty"));
				food.setFurl(rs.getString("furl"));
				food.setFnew(rs.getInt("fnew"));
				food.setFprice(rs.getDouble("fprice"));
				food.setFmaterial(rs.getString("fmaterial"));
				food.setFhasma(rs.getInt("fhasma"));
				li.add(food);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcConUtilM.closePstAndCon(ps, con);
		}
		return li;
	}

	@Override
	public int selectLikeCont(String like) {
		int flag = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("select COUNT(fid) scount from o_food");
		sql.append(" where fname LIKE ?  and fhasma = 1"); /*and fhasma = 0*/
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement ps =null;
		try {
			 ps = con.prepareStatement(sql.toString());
			 ps.setString(1, "%"+like+"%");
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				flag = rs.getInt("scount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcConUtilM.closePstAndCon(ps, con);
		}
		return flag;
	}

	@Override
	public boolean insetsFoodsAnduid(String foods, int id, int did, String time,int fnub,int fstate) {
		StringBuffer sql = new StringBuffer();
		boolean flag = false;
		sql.append("INSERT INTO o_desk (foodlist,wid,did,ftime,fnum,fstate)");
		sql.append(" VALUES (?,?,?,?,?,?)");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement ps = null;
		try {
			 ps = con.prepareStatement(sql.toString());
			 ps.setString(1, foods);
			 ps.setInt(2, id);
			 ps.setInt(3, did);
			 ps.setString(4,time);
			 ps.setInt(5, fnub);
			 ps.setInt(6, fstate);
			 int rs = ps.executeUpdate();
			 if(rs>0){
				flag = true;
				
			} 
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcConUtilM.closePstAndCon(ps, con);
		}
		
		return flag;
	}

	@Override
	public FoodX selectFnameAndfhasmaById(int fid) {
		FoodX fo =null;
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT fname,fhasma,fid FROM o_food");
		sql.append(" WHERE fid = ? and fhasma = 0 ");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement(sql.toString());
			ps.setInt(1, fid);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				fo = new FoodX();
				fo.setFid(rs.getInt("fid"));
				fo.setFname(rs.getString("fname"));
				fo.setFhasma(rs.getInt("fhasma"));				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcConUtilM.closePstAndCon(ps, con);
		}
		return fo;
	}

	

	


	
}
